/*
 * Copyright 2007 National Bioinformatics Network
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package dgbgui.gui;

import java.util.LinkedList;
import java.util.Observable;
import java.util.Observer;

import dgbgui.gui.event.CheckConsumedInputEventHandler;
import edu.umd.cs.piccolo.PCamera;
import edu.umd.cs.piccolo.PCanvas;
import edu.umd.cs.piccolo.event.PInputEvent;
import edu.umd.cs.piccolo.event.PInputEventListener;

/**
 * @author bjnortier
 * Abstract Graphics view, contains the camera of the view, and does updates of the
 * view on scene changes.
 */
public abstract class GraphicsView extends PCanvas implements Observer, PInputEventListener {

    public GraphicsView (GraphicsScene scene) {         
        // Create a new canvas
        PCamera camera = new PCamera();
        camera.addLayer(scene.getLayer());
        getRoot().addChild(camera);
        setCamera(camera);
        
        addInputEventListener(this);
    }
    
    // Observer method (observing the graphics scene)
    public void update(Observable source, Object data)  {
        repaint();
    }
    

    public void processEvent(PInputEvent event, int type) {
        // Handle event along the list until it is processed
        for ( CheckConsumedInputEventHandler handler : m_inputEventHandlers ) {
            if ( handler.eventProcessed(event, type) ) {
                return;
            }
        }
    }
        
    // The input event list. The order of the list of input event handlers is
    // important, as input event are processed in the list order, and only 
    // sent to the next handler if the event wasn't handled. This avoids 
    // confusin behaviour and simplifies dependencies.
    protected LinkedList<CheckConsumedInputEventHandler> m_inputEventHandlers = new LinkedList<CheckConsumedInputEventHandler>();
    
}
